package com.mcr.graduateprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mcr.graduateprogram.bean.Clientele;
import com.mcr.graduateprogram.mapper.ClienteleMapper;
import com.mcr.graduateprogram.service.IClienteleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author graduateprogram
 * @since 2023-02-25
 */
@Service
public class ClienteleServiceImpl extends ServiceImpl<ClienteleMapper, Clientele> implements IClienteleService {
    @Autowired
    private ClienteleMapper clienteleMapper;
    @Value("${clientele.defaultPhotoURL}")
    private String defaultPhotoURL;
    @Override
    public Page<Clientele> getPage(Integer page, Integer pageSize,String name) {
        Page<Clientele> clientelePage=new Page<>(page,pageSize);
        LambdaQueryWrapper<Clientele> clienteleLambdaQueryWrapper=new LambdaQueryWrapper<>();
        clienteleLambdaQueryWrapper.like(name!=null,Clientele::getName,name);
        clienteleMapper.selectPage(clientelePage,clienteleLambdaQueryWrapper);
        return clientelePage;
    }

    @Override
    public boolean updateClienteleById(Clientele clientele) {
        List<Clientele> clienteleList = checkName(clientele);
        if((clienteleList.size()==0)||(clienteleList.size()==1&&clienteleList.get(0).getId().longValue()==clientele.getId().longValue())){
            clienteleMapper.updateById(clientele);
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean registerClientele(Clientele clientele) {
        List<Clientele> clienteleList = checkName(clientele);
        if(clienteleList.size()==0&&
                clientele.getName()!=null&&clientele.getPassword()!=null&&
                clientele.getName()!=""&&clientele.getPassword()!=""){
            clientele.setGameMoney(0);
            clientele.setProfilePhoto(defaultPhotoURL);
            clienteleMapper.insert(clientele);
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Clientele loginClientele(Clientele clientele) {
        LambdaQueryWrapper<Clientele> clienteleLambdaQueryWrapper=new LambdaQueryWrapper<>();
        clienteleLambdaQueryWrapper.eq(Clientele::getName,clientele.getName())
                           .eq(Clientele::getPassword,clientele.getPassword());
        return clienteleMapper.selectOne(clienteleLambdaQueryWrapper);
    }

    private List<Clientele> checkName(Clientele clientele){
        LambdaQueryWrapper<Clientele> clienteleLambdaQueryWrapper=new LambdaQueryWrapper<>();
        clienteleLambdaQueryWrapper.eq(Clientele::getName,clientele.getName());
        return clienteleMapper.selectList(clienteleLambdaQueryWrapper);
    }
}
